﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.GUI;
using ITAP.ContourBuilder.Components;
using System.Drawing;

using ITAP.Core.Calculations;
using ITAP.Core.Modelling.Primitives;
using System.Windows.Forms;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandCreateCutout : SerializedCommand
    {
        public CommandCreateCutout(ContourControl control)
            : base(control)
        {
        }

        ContourElement element = null;
        FormCreateCutout form = null;

        private double startOffset;
        private double endOffset;

        IPrimitive offsetPrimitive = null;

        public override void Execute()
        {
            base.Execute();
            foreach(IContourComponent component in this.control.ContourContainer.SelectedComponentsStack)
                if(component is ContourElement)
                {
                    element = component as ContourElement;
                    break;
                }
            if(element == null)
            {
                this.Cancel();
                return;
            }

            if (element.Height != 0)
            {
                MessageBox.Show("Вырез можно создать только на прямой стороне.");
                this.Cancel();
                return;
            }
            form = new FormCreateCutout(element.Length);
            form.ParametersChanged += new CutoutParametersChangedDelegate(form_ParametersChanged);

            offsetPrimitive = element.Primitive.GetOffsetPrimitive(-form.CutoutDepth);

            startOffset = form.CutoutStart / element.Length;
            endOffset = (form.CutoutStart + form.CutoutLength) / element.Length;


            startPoint = this.element.Primitive.GetPoint(startOffset);
            startCutoutPoint = offsetPrimitive.GetPoint(startOffset);
            endCutoutPoint = offsetPrimitive.GetPoint(endOffset);
            endPoint = this.element.Primitive.GetPoint(endOffset);
            this.control.Invalidate();
            if (form.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                this.Cancel();
                return;
            }
            else
            {
                ModelPoint startElementPoint = element.StartPoint;
                ModelPoint endElementPoint = element.EndPoint;

                ContourModel model = (ContourModel)element.Parent;
                element.Parent.RemoveInnerComponent(element);
                ContourElement element1 = ContourElementFactory.CreateLineContourElement(startElementPoint, startPoint, "");
                ContourElement element2 = ContourElementFactory.CreateLineContourElement(startPoint, startCutoutPoint, "");
                ContourElement element3 = ContourElementFactory.CreateLineContourElement(startCutoutPoint, endCutoutPoint, "");
                ContourElement element4 = ContourElementFactory.CreateLineContourElement(endCutoutPoint, endPoint, "");
                ContourElement element5 = ContourElementFactory.CreateLineContourElement(endPoint, endElementPoint, "");

                model.AddInnerComponent(element1);
                model.AddInnerComponent(element2);
                model.AddInnerComponent(element3);
                model.AddInnerComponent(element4);
                model.AddInnerComponent(element5);
                this.control.UpdateModel();
                this.control.ToleranceManager.Visit(this.control.ContourContainer);
                this.control.UpdateModel();
                this.Exit();
            }
        }

        ModelPoint startPoint = null;
        ModelPoint startCutoutPoint = null;
        ModelPoint endCutoutPoint = null;
        ModelPoint endPoint = null;

        public override void DrawOnGraphics(Graphics g, int screenWidth, int screenHeight, RectangleF modelRectangle, ModelPoint cameraCenter, ModelCoordinateConverter coordinateConverter)
        {
            if (element == null || offsetPrimitive == null)
                return;

            PointF startPointScreen = this.ModelPointToScreenPoint(startPoint);
            PointF startCutoutPointScreen = this.ModelPointToScreenPoint(startCutoutPoint);
            PointF endCutoutPointScreen = this.ModelPointToScreenPoint(endCutoutPoint);
            PointF endPointScreen = this.ModelPointToScreenPoint(endPoint);
            g.DrawLine(Pens.DarkGray, startPointScreen, startCutoutPointScreen);
            g.DrawLine(Pens.DarkGray, startCutoutPointScreen, endCutoutPointScreen);
            g.DrawLine(Pens.DarkGray, endCutoutPointScreen, endPointScreen);
        }

        void form_ParametersChanged()
        {
            startOffset = form.CutoutStart / element.Length;
            endOffset = (form.CutoutStart + form.CutoutLength) / element.Length;


            startPoint = this.element.Primitive.GetPoint(startOffset);
            startCutoutPoint = offsetPrimitive.GetPoint(startOffset);
            endCutoutPoint = offsetPrimitive.GetPoint(endOffset);
            endPoint = this.element.Primitive.GetPoint(endOffset);

            offsetPrimitive = element.Primitive.GetOffsetPrimitive(-form.CutoutDepth);
            this.control.Invalidate();
        }

        public override string ToString()
        {
            return "Создать вырез";
        }
    }
}
